package com.authine.cloudpivot.ext.model.goods;

import com.authine.cloudpivot.ext.entity.goods.GoodsFullEntity;
import com.authine.cloudpivot.ext.enums.ReviewStatusEnum;
import com.authine.cloudpivot.ext.enums.goods.GoodsType;
import com.authine.cloudpivot.ext.enums.goods.MallFeaturedEnum;
import com.authine.cloudpivot.ext.utils.Utils;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import javax.persistence.Column;
import javax.persistence.Transient;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;

/**
 * @Author: zhoujian
 * @Date: 2019/12/10 15:20
 * @Version 1.0
 */
@Data
@Builder
@ToString
//@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@RequiredArgsConstructor(staticName = "of")
public class GoodsFullModel implements Serializable {
    private static final long serialVersionUID = 2453266788019981642L;
    @ApiModelProperty(value = "商品id", required = true, dataType = "String")
    @JsonSerialize(using = ToStringSerializer.class)
    private String id;
    @ApiModelProperty(value = "商品类型", required = true, dataType = "object")
    @Column
    private GoodsType type;
    @ApiModelProperty(value = "商城商品审核状态", required = false, dataType = "object")
    private ReviewStatusEnum reviewStatus;
    @ApiModelProperty(value = "商城商品类型 0: 普通商品 1: 精选商品", required = false, dataType = "object")
    private MallFeaturedEnum mallFeatured;
    @ApiModelProperty(value = "供应商ID", required = true, dataType = "String")
    private String vendorId;
    @ApiModelProperty(value = "供应商名称", required = true, dataType = "string")
    private String orgFullname;
    //    private String vendorName;
//    @ApiModelProperty(value = "目录ID", required = true, dataType = "String")
//    private Long catalogId;
    @ApiModelProperty(value = "目录", required = true, dataType = "string")
    private String catalog;
    @ApiModelProperty(value = "单位", required = true, dataType = "string")
    private String unit;
    @ApiModelProperty(value = "包装单位", required = true, dataType = "string")
    private String packageUnit;
    @ApiModelProperty(value = "包装数量", required = true, dataType = "double")
    private Double packageNum;
    @ApiModelProperty(value = "税前商品单价", required = true, dataType = "double")
    private Double pretaxPrice;
    @ApiModelProperty(value = "税后商品单价", required = false, dataType = "double")
    private Double price;
    @ApiModelProperty(value = "税率", required = false, dataType = "double")
    private Double taxRate;
    @ApiModelProperty(value = "sku", required = true, dataType = "string")
    private String vendorSku;
    @ApiModelProperty(value = "库存（NULL:不限制库存量）", required = false, dataType = "integer")
    private Double stock;
    @ApiModelProperty(value = "保修期", required = true, dataType = "string")
    private String warranty;
    @ApiModelProperty(value = "无库存交货期(工作日)", required = true, dataType = "integer")
    private Integer delivery;
    @ApiModelProperty(value = "最小起订量", required = true, dataType = "double")
    private Double minOrder;
    @ApiModelProperty(value = "过期时间（NULL: 不限制过期时间）", required = false, dataType = "date")
    private Date expiry;
    @ApiModelProperty(value = "长", required = false, dataType = "double")
    private Double length;
    @ApiModelProperty(value = "宽", required = false, dataType = "double")
    private Double width;
    @ApiModelProperty(value = "高", required = false, dataType = "double")
    private Double height;
    @ApiModelProperty(value = "重量", required = false, dataType = "double")
    private Double weight;
    @ApiModelProperty(value = "运费模板ID", required = false, dataType = "integer")
    private Integer freightTemplateId;
    @ApiModelProperty(value = "供应商上下架", required = true, dataType = "boolean")
    private Boolean saled;
    @ApiModelProperty(value = "商城中上下架", required = true, dataType = "boolean")
    private Boolean mallSales;
    @ApiModelProperty(value = "供应商启禁用", required = true, dataType = "boolean")
    private Boolean goodsEnabled;
    @ApiModelProperty(value = "货期要求", required = false, dataType = "string")
    private String deliveryClaim;
    @ApiModelProperty(value = "客户端ID", required = false, dataType = "string")
    private String clientId;
    @ApiModelProperty(value = "客户商品ID", required = false, dataType = "string")
    private String clientGoodsId;
    private String deliveryPlace;
    @ApiModelProperty(value = "现货货期", required = false, dataType = "integer")
    private Integer stockDelivery;
    @ApiModelProperty(value = "安全库存", required = false, dataType = "integer")
    private Integer safeStock;
    @ApiModelProperty(value = "是否为核心品牌", required = false, dataType = "boolean")
    private Boolean core;
    @ApiModelProperty(value = "审批意见", required = false, dataType = "string")
    private String reviewContent;

    /**
     * 商品特征
     **/
    @ApiModelProperty(value = "分类", required = true, dataType = "string")
    private String category;
    @ApiModelProperty(value = "品牌", required = true, dataType = "string")
    private String brand;
    @ApiModelProperty(value = "商品名称", required = true, dataType = "string")
    private String name;
    @ApiModelProperty(value = "型号", required = true, dataType = "string")
    private String model;
    @ApiModelProperty(value = "规格", required = true, dataType = "string")
    private String spec;
    @ApiModelProperty(value = "平台SKU", required = true, dataType = "string")
    private String platformSku;
    @ApiModelProperty(value = "条形码", required = true, dataType = "string")
    private String barCode;
    @ApiModelProperty(value = "金税编码", required = true, dataType = "string")
    private String goldenTax;
    @ApiModelProperty(value = "美国电子目录编码", required = true, dataType = "string")
    private String unspsc;
    @ApiModelProperty(value = "启禁用", required = true, dataType = "boolean")
    private Boolean profileEnabled;

    /**
     * 商品详情
     **/
    @ApiModelProperty(value = "产地", required = true, dataType = "string")
    private String place;
    @ApiModelProperty(value = "图片集", required = true, dataType = "string")
    private String images;
    @ApiModelProperty(value = "商品描述", required = true, dataType = "string")
    private String description;
    @ApiModelProperty(value = "参数信息", required = true, dataType = "string")
    private String technical;
    @ApiModelProperty(value = "包装清单", required = true, dataType = "string")
    private String packaging;
    @ApiModelProperty(value = "备注", required = true, dataType = "string")
    private String memo;

    @ApiModelProperty(value = "调整后税前商品单价", dataType = "double")
    private Double adjustPretaxPrice;
    @ApiModelProperty(value = "调整后税后商品单价", dataType = "double")
    private Double adjustPrice;
    @ApiModelProperty(value = "调整比例", dataType = "double")
    private Double adjustRatio;

    @Transient
    @ApiModelProperty(value = "是否已经计算商城价格", required = true, dataType = "string")
    private Boolean calculatePrice;


    public static GoodsFullModel entityToModel(GoodsFullEntity entity) {

        GoodsFullModel build = GoodsFullModel.builder()
                .id(entity.getId())
                .type(GoodsType.get(Utils.objToInteger(entity.getType())))
                .reviewStatus(ReviewStatusEnum.getEnum(Utils.objToInteger(entity.getReview_status())))
                .mallFeatured(MallFeaturedEnum.get(Utils.objToInteger(entity.getMall_featured())))
                .vendorId(Utils.getSelectorFormatId(entity.getVendor_id()))
                .orgFullname(Utils.getSelectorFormatName(entity.getVendor_id()))
                .catalog(entity.getCatalog())
                .unit(entity.getUnit())
                .packageUnit(entity.getPackage_unit())
                .packageNum(Optional.ofNullable(entity.getPackage_num()).orElse(BigDecimal.ZERO).doubleValue())
                .pretaxPrice(Optional.ofNullable(entity.getPretax_price()).orElse(BigDecimal.ZERO).doubleValue())
                .price(Optional.ofNullable(entity.getPrice()).orElse(BigDecimal.ZERO).doubleValue())
                .taxRate(Optional.ofNullable(entity.getTax_rate()).orElse(BigDecimal.ZERO).doubleValue())
                .vendorSku(entity.getSku())
                .stock(Optional.ofNullable(entity.getStock()).orElse(BigDecimal.ZERO).doubleValue())
                .delivery(Utils.objToInteger(entity.getDelivery()))
                .minOrder(Optional.ofNullable(entity.getMin_order()).orElse(BigDecimal.ZERO).doubleValue())
                .expiry(DateUtils.strToDate(entity.getExpiry()))
                .length(Optional.ofNullable(entity.getLength()).orElse(BigDecimal.ZERO).doubleValue())
                .width(Optional.ofNullable(entity.getWidth()).orElse(BigDecimal.ZERO).doubleValue())
                .height(Optional.ofNullable(entity.getHeight()).orElse(BigDecimal.ZERO).doubleValue())
                .weight(Optional.ofNullable(entity.getWeight()).orElse(BigDecimal.ZERO).doubleValue())
                .freightTemplateId(Utils.objToInteger(entity.getFreight_template_id()))
                .saled(entity.getSaled())
                .mallSales(entity.getMall_sales())
                .goodsEnabled(entity.getEnabled())
                .deliveryClaim(entity.getDelivery_claim())
                .clientId(entity.getClient_id())
                .clientGoodsId(entity.getClient_goods_id())
                .deliveryPlace(entity.getDelivery_place())
                .stockDelivery(Utils.objToInteger(entity.getStock_delivery()))
                .safeStock(Utils.objToInteger(entity.getSafe_stock()))
                .core(entity.getCore())
                .reviewContent(entity.getReview_content())
                .category(entity.getCategory())
                .brand(entity.getBrand())
                .name(entity.getName())
                .model(entity.getModel())
                .spec(entity.getSpec())
//                .platformSku(entity.getSku())
                .barCode(entity.getBar_code())
                .goldenTax(entity.getGolden_tax())
                .unspsc(entity.getUnspsc())
//                .profileEnabled(entity.getEnabled())
                .place(entity.getPlace())
//                .images(entity.getImages().toString())
                .description(entity.getDescription())
                .technical(entity.getTechnical())
                .packaging(entity.getPackaging())
                .memo(entity.getMemo())
                .adjustPretaxPrice(Optional.ofNullable(entity.getAdjust_pretax_price()).orElse(BigDecimal.ZERO).doubleValue())
                .adjustPrice(Optional.ofNullable(entity.getAdjust_price()).orElse(BigDecimal.ZERO).doubleValue())
                .adjustRatio(Optional.ofNullable(entity.getAdjust_ratio()).orElse(BigDecimal.ZERO).doubleValue())
                .build();

        return build;
    }
}
